package com.tbfs.util;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JSONObject;

public class UtilTool {
	
	public  static String formatDate(Date date){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);
	}
	
	public static int parseInt (String value){
		int rInt = 0;
		if(value!=null && !"".equals(value)){
			rInt = Integer.parseInt(value);
		}
		return rInt;
	}
	public static String decimalFormat(double d){
		String r = "0.00";
		if(!"".equals(d)){
			DecimalFormat df = new DecimalFormat("#.##");    
			r = df.format(d);
		}
		return r;
	}
	/**
	 * double 保留两位小数 四舍五入
	 * @param d
	 * @return
	 */
	public static double doubleFormat(double d){
		BigDecimal   b   =   new   BigDecimal(d);  
		double   f   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
		return f;
	}
	
	
	
	public static double string2doubleFormat(String d){
		double r = 0d;
		if(d==null || "".equals(d )){
			return r;
		}	
		return Double.valueOf(d);
	}

	/**
	 * 取出jsonString字符串 填充对象
	 * 
	 * @param jsonString
	 * @param o
	 * @return
	 */
	public static Object parseJsonStr2Obj(String jsonString, Class clazz) {

		JSONObject jo = JSONObject.fromObject(jsonString);// 格式化成json对象

		Object object = null;
		try {
			object = clazz.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();

		}

		// 封装
		Field[] declaredFields = clazz.getDeclaredFields();

		if (declaredFields != null && declaredFields.length > 0) {
			for (int i = 0; i < declaredFields.length; i++) {
				Field field = declaredFields[i];
				String name = field.getName();
				//id 转换
				/*if("item_id".equals(name)){
					name = "id";
				}*/
				
				Object  parseObj = jo.get(name);
				
				if(parseObj!=null && !"".equals(parseObj)){
				
					field.setAccessible(true);
					Class propertyType = field.getType();
					if (propertyType == Integer.class
							|| propertyType.getName().equals("int")) // 处理为int或者Integer的情况。
					{
						int value = 0;
						if(parseObj instanceof Integer){
							 value = ((Integer)parseObj);
							 try {
								field.set(object, value);
							} catch (IllegalArgumentException e) {
								e.printStackTrace();
							} catch (IllegalAccessException e) {
								e.printStackTrace();
							}
						}else{
							try {							
								field.setInt(object, value);
							} catch (Exception e) {
							}
						}
						
						
					}
					if (propertyType == String.class) // 处理为String的情况
					{
						String value = "";
						if(parseObj instanceof String){
							 value = ((String)parseObj);
						}
						if(parseObj instanceof Double){
							 value = String.valueOf(((Double)parseObj));
						}
						
						try {
							field.set(object, value);
						} catch (IllegalArgumentException e) {
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						}
					}
	
					// 封装的Double类型
					if (propertyType == Double.class
							|| propertyType.getName().equals("double")) // 处理为double或者Double的情况。
					{
						double value=0.0 ;
						if(parseObj instanceof Double){
							 value = ((Double)parseObj).doubleValue();
						}
						if(parseObj instanceof Integer){
							 value = ((Integer)parseObj).doubleValue();
						}
								
						try {
							
							field.setDouble(object, value);
						} catch (Exception e) {
						}
					}
	
				}
	
			}
		}	
		
		
		return object;
	}

}
